﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WebCore.ConfigHelper
{
    /// <summary>
    /// appsettings.json 文件内容获取
    /// </summary>
    public class AppsettingsConfigHelper
    {
        private static readonly IConfiguration _configuration;

        static AppsettingsConfigHelper()
        {
            var fileName = "appsettings.json";
            var directory = AppContext.BaseDirectory;
            directory = directory.Replace("\\", "/");

            var filePath = $"{directory}/{fileName}";
            if (!File.Exists(filePath))
            {
                var length = directory.IndexOf("/bin");
                filePath = $"{directory.Substring(0, length)}/{fileName}";
            }
            var builder = new ConfigurationBuilder()
                .AddJsonFile(filePath, false, true);
            _configuration = builder.Build();
        }

        /// <summary>
        /// 获取Section的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetSectionValue(string key)
        {
            return _configuration.GetSection(key).Value;
        }

        /// <summary>
        /// 获取ConnectionStrings下的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionString(string key)
        {
            return _configuration.GetConnectionString(key);
        }
    }
}
